package com.enbatis.mybatisplugs.service.impl;

import com.enbatis.mybatisplugs.base.BaseServiceImpl;

import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.entity.SysResource;
import com.enbatis.mybatisplugs.entity.dto.Menu;
import com.enbatis.mybatisplugs.entity.dto.MenuDTO;
import com.enbatis.mybatisplugs.entity.vo.SysResourceVO;
import com.enbatis.mybatisplugs.mapper.SysResourceMapper;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.enbatis.mybatisplugs.service.SysResourceService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
* 描述：
* @author wangweidong
* @date 2019-06-18 12:41:26
*/

@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResourceMapper,SysResource > implements SysResourceService {


    @Override
    public List<MenuDTO> getMenuList(SysResource sysResource) {
        sysResource.setPid(0L);
        List<SysResource> resourceList= this.list(new Wrapper<>(sysResource));
        List<MenuDTO> dtoList=new ArrayList<>();
        for (SysResource resource:resourceList) {
            sysResource.setPid(resource.getId());
            List<SysResource> childList=this.list(new Wrapper<>(sysResource));
            MenuDTO dto=new MenuDTO();
            dto.setMenuName(resource.getName());
            dto.setPath(resource.getUrl());
            dto.setName(resource.getCode());
            dto.setPid(resource.getPid());
            dto.setIcon(resource.getPic());
            if (!CollectionUtils.isEmpty(childList)){
                List<Menu> menusList=new ArrayList<>();
                for (SysResource r:childList) {
                    Menu menu=new Menu();
                    menu.setMenuName(r.getName());
                    menu.setPath(r.getUrl());
                    menu.setName(r.getCode());
                    menu.setPid(r.getPid());
                    menu.setIcon(r.getPic());
                    menusList.add(menu);
                }
                dto.setChildren(menusList);
            }
            dtoList.add(dto);
        }
        return dtoList;
    }



    @Override
    public List<MenuDTO> getMenuVOList(SysResourceVO vo) {
        List<SysResourceVO> resourceVOList=baseMapper.selectVOByRoleList(vo);

        List<MenuDTO> dtoList=new ArrayList<>();

        for (SysResourceVO resourceVO:resourceVOList) {
            if (resourceVO.getPid()==0){
                MenuDTO dto=new MenuDTO();
                dto.setId(resourceVO.getId());
                dto.setLabel(resourceVO.getName());
                dto.setChecked(resourceVO.getChecked());
                dto.setPid(resourceVO.getPid());
                dtoList.add(dto);
            }
        }

        for (MenuDTO menuDTO:dtoList) {
            List<Menu> menusList=new ArrayList<>();
            for (SysResourceVO resourceVO:resourceVOList){
                if (menuDTO.getId().equals(resourceVO.getPid())){
                      Menu menu=new Menu();
                      menu.setId(resourceVO.getId());
                      menu.setLabel(resourceVO.getName());
                      menu.setChecked(resourceVO.getChecked());
                      menu.setPid(resourceVO.getPid());
                      menusList.add(menu);
                }
            }
            menuDTO.setChildren(menusList);
        }


        return dtoList;
    }


    @Override
    public Pages<SysResourceVO> pageVO(Page<SysResourceVO> page, SysResourceVO vo) {
        Page returnPage= PageHelper.startPage(page.getPageNum(),page.getPageSize(),page.getOrderBy());
        baseMapper.selectVOList(vo);
        return new Pages<>(returnPage);
    }



}
